package cn.edu.sjtu.se.dslab.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.project.BusZone;
import cn.edu.sjtu.se.dslab.project.EntityZone;
import cn.edu.sjtu.se.dslab.service.IBusZoneService;
import cn.edu.sjtu.se.dslab.service.IDesignPatternComponentService;
import cn.edu.sjtu.se.dslab.service.IEntityZoneService;
import cn.edu.sjtu.se.dslab.service.IProjectService;

@Controller
@RequestMapping(value="/busZone")
public class BusZoneController {

	private static final Logger logger = Logger
			.getLogger(BusZoneController.class);
	
	@Resource(name = "entityZoneService")
	IEntityZoneService entityZoneService;
	
	@Resource(name = "busZoneService")
	IBusZoneService busZoneService;
	
	@Resource(name = "projectService")
	IProjectService projectService;

	@Resource(name = "designPatternComponentService")
	IDesignPatternComponentService designPatternComponentService;
	
	@RequestMapping(value = "/{type}/{projectId}/add", method = RequestMethod.POST)
	public String addBusZone(@PathVariable(value = "projectId")long projectId,
			@PathVariable(value = "type")String type, BusZone busZone, String[] entityZoneId){
		busZone.addEntityZones(entityZoneId);
		busZoneService.addBusZone(busZone, projectId);
		StringBuilder sb = new StringBuilder();
		sb.append("redirect:/").append(type).append("/").append(projectId).append("/design_pattern_component/BUS/edit");
		return sb.toString();
	}
	
	@RequestMapping(value = "/{projectId}/{busZoneId}", method = RequestMethod.DELETE)
	@ResponseBody
	public Map<String, String> deleteBusZone(@PathVariable(value = "projectId")long projectId, 
			@PathVariable(value = "busZoneId")long busZoneId){
		Map<String, String> responseMap = new HashMap<String, String>();
		try {
			logger.info("Delete Bus Zone" + busZoneId);
			boolean result = busZoneService.deleteBusZoneById(busZoneId);
			if (result) {
				responseMap.put("result", Boolean.TRUE.toString());
			} else {
				responseMap.put("result", Boolean.FALSE.toString());
			}
		} catch (Exception ex) {
			responseMap.put("result", Boolean.FALSE.toString());
		}
		return responseMap;
	}
	
	@RequestMapping(value = "/{busZoneId}", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Set<EntityZone>> viewBusZone(@PathVariable(value = "busZoneId")long busZoneId){
		Map<String, Set<EntityZone>> responseMap = new HashMap<String, Set<EntityZone>>();
		logger.info("BusZone id " + busZoneId);
		Set<EntityZone> entityZones = busZoneService.getEntityZoneSet(busZoneId);
		for (EntityZone entityZone : entityZones){
			entityZone.setProject(null);
			entityZone.getEntityZoneCategory().setProject(null);
		}
		responseMap.put("entityZones", entityZones);
		return responseMap;
	}
	

	@ExceptionHandler({ IOException.class, DBException.class })
	public ResponseEntity<String> handleIOException(IOException ex) {
		// prepare responseEntity
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.TEXT_PLAIN);
		ResponseEntity<String> responseEntity = new ResponseEntity<String>(
				"The server has some errors,please try later!", headers,
				HttpStatus.OK);
		return responseEntity;
	}
	
	
	
	
	
	
}
